package com.ly.quickdev.library.manager;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.google.gson.Gson;
import com.ly.quickdev.library.bean.BaseUserIface;
import com.ly.quickdev.library.utils.JsonUtils;
import com.yunfeng.supercall.bean.UserBean;
import com.t9search.model.PinyinUnit;
public class LoginManager<T extends BaseUserIface> {

	public boolean isLogined() {
		return mLogined;
	}

	private T mT;

	protected boolean mLogined;

	public LoginManager(Context context) {
		mPreferences = context.getSharedPreferences("user",
				Context.MODE_PRIVATE);
	}

	private SharedPreferences mPreferences;

	private LoginManager() {

	}

	public void saveUser(T t) {
		mPreferences.edit().putString("user", new Gson().toJson(t)).commit();
		mT = t;
		mLogined = true;
	}

	public void saveUser(T t, boolean saveToDisk) {
		clearLoginState();
		if (saveToDisk) {
			saveUser(t);
		} else {
			mT = t;
			mLogined = true;
		}
	}

	public T getCurrentUser(Class<T> class1) {
		if (mT != null) {
			return mT;
		}
		String canonical = class1.getCanonicalName();
		Log.i("supercall", canonical);
//		boolean res = class1.get instanceof User; 
//		if() { 
		if(!canonical.equals("com.yunfeng.supercall.bean.UserBean")) {
			mT = JsonUtils.parse(mPreferences.getString("user", ""), class1);
		} else {
			String jsonString = mPreferences.getString("user", "");
			try {
				JSONObject jsonObject = new JSONObject(jsonString);
				UserBean<T> user = new UserBean<T>();
				user.setAddress(jsonObject.getString("address"));
				user.setVo_pass(jsonObject.getString("vo_pass"));
				user.setCard(jsonObject.getString("card"));
				user.setVo_id(jsonObject.getString("vo_id"));
				user.setPassword(jsonObject.getString("password"));
				user.setNote(jsonObject.getString("note"));
				user.setCalltime(jsonObject.getInt("calltime"));
				user.setLng(jsonObject.getDouble("lng"));
				user.setLat(jsonObject.getDouble("lat"));
				user.setSex(jsonObject.getInt("sex"));
				user.setLevel(jsonObject.getInt("level"));
				user.setSuperNumber(jsonObject.getString("superNumber"));
				user.setMatchKeywords(jsonObject.getString("mMatchKeywords"));
				ArrayList<PinyinUnit> list = new ArrayList<PinyinUnit>();
				String pingyin = jsonObject.getString("mNamePinyinUnits");
				JSONArray jsonArray = new JSONArray(pingyin);
				for(int i=0;i < jsonArray.length(); i++) {
					
				}
				
				user.setNamePinyinUnits(list); //数组
				user.setNick_name(jsonObject.getString("nick_name"));
				user.setOnlineday(jsonObject.getString("onlineday"));
				user.setPhone(jsonObject.getString("phone"));
				user.setPhoto(jsonObject.getString("photo"));
				user.setReal_name(jsonObject.getString("real_name"));
				user.setSuid(jsonObject.getString("suid"));
				user.setLastChatTime(jsonObject.getLong("lastChatTime"));
				user.setSucolor(jsonObject.getInt("sucolor"));
				user.setType(jsonObject.getInt("type"));
				user.setUnread(jsonObject.getBoolean("unread"));
				user.setUnreadCount(jsonObject.getInt("unreadCount"));
				user.setUserid(jsonObject.getInt("userid"));
				return (T) user;
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return mT;
	}

	public void clearLoginState() {
		mPreferences.edit().clear().commit();
		mT = null;
		mLogined = false;
	}

	public SharedPreferences getSharedPreferences() {
		return mPreferences;
	}
}
